package com.junwell.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.junwell.common.dto.PageDto;
import com.junwell.common.lang.PageResult;
import com.junwell.entity.Orders;
import com.junwell.mapper.OrdersMapper;
import com.junwell.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 */
@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements OrdersService {

    @Autowired
    private  OrdersMapper ordersMapper;


    @Transactional
    @Override
    public PageResult findPage(PageDto pageDto) {
        Page<Orders> page=new Page<>(pageDto.getCurrentPage(),pageDto.getPageSize());
        QueryWrapper<Orders> queryWrapper=new QueryWrapper<>();
        String query=pageDto.getQueryString();
        if (StringUtils.hasLength(query)){
            queryWrapper.or().eq("id",pageDto.getQueryString());
            queryWrapper.or().like("id",pageDto.getQueryString());
        }
        List<Orders> orders = ordersMapper.selectList(queryWrapper);
        return new PageResult(page.getTotal(),orders);
    }

    @Transactional
    @Override
    public boolean add(Orders orders) {
        int i = ordersMapper.insert(orders);
        return  i>0? true:false;
    }



    @Transactional
    @Override
    public Orders viewOne(Integer id) {
        QueryWrapper<Orders> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        Orders orders = ordersMapper.selectOne(queryWrapper);
        return orders;
    }


    @Transactional
    @Override
    public boolean update(Orders orders) {
        int i = ordersMapper.updateById(orders);
        return  i>0? true:false;
    }


    @Transactional
    @Override
    public boolean delete(Integer id) {
        int i = ordersMapper.deleteById(id);
        return  i>0? true:false;
    }

    @Transactional
    public boolean updateList(List<Orders> ordersList){
        int i = ordersMapper.updateDriver(ordersList);
        return i>0? true:false;
    }

    @Transactional
    public boolean driverSentOver(Integer id){
        int i = ordersMapper.driverSentOver(id);
        return i>0? true:false;
    }

    @Transactional
    public boolean sent(Integer id){
        int i = ordersMapper.sent(id);
        return i>0? true:false;
    }



    @Transactional
    public boolean over(Integer id){
        int i = ordersMapper.over(id);
        return i>0? true:false;
    }
}
